package cn.wolfcode.web.controller;

import cn.wolfcode.anno.RequiredPermission;
import cn.wolfcode.query.JSONResult;
import cn.wolfcode.query.QueryObject;
import cn.wolfcode.service.IPermissionService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller // 标识一下 可以被扫描 创建对象 加入spring容器
@RequestMapping("/permission") // 访问路径的映射地址   作用 ： 可以通过浏览器 进行访问的地址
public class PermissionController {

    @Autowired// 自动装配  DepartmentService
    private IPermissionService permissionService;

    @RequestMapping("/list")
    @RequiredPermission({"权限列表权限","PermissionController.list"})
    public String list(Model model, @ModelAttribute("qo") QueryObject qo) {  // 返回值是String   返回值的内容 一般是一个地址
        PageInfo pageInfo = permissionService.findAll(qo);
        // 把数据共享给页面
        model.addAttribute("pageInfo", pageInfo);

        return "/permission/list";
    }

    @RequestMapping("/reload")
    @RequiredPermission({"权限重新加载权限","PermissionController.reload"})
    public String reload(){
            permissionService.reload();
        return "redirect:/permission/list";
    }
    @RequestMapping("/delete")
    @ResponseBody
    @RequiredPermission({"权限删除权限","RoleController.delete"})

    public JSONResult delete(Long id) {
        try {
            permissionService.deleteById(id);
            return new JSONResult();
        } catch (Exception e){
            e.printStackTrace();
            return new JSONResult().mark("删除失败，请稍后重试");
        }

    }
}
